package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.system.model.entity.SysNotice;
import cn.lingyangwl.agile.system.model.rqrs.notice.*;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.agile.system.service.SysNoticeService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 公告 信息操作处理
 * 
 * @author shenguangyang
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController {
    @Resource
    private SysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @RamResource(action = "sys:notice:query", name = "查询通知公告", type = ActionType.READ)
    @GetMapping("/page")
    public Resp<IPage<SysNotice>> list(NoticeQueryReq req) {
        IPage<SysNotice> page = noticeService.pageNotice(req);
        return Resp.ok(page);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping(value = "/get")
    @RamResource(action = "sys:notice:query", name = "查询通知公告", type = ActionType.READ)
    public Resp<SysNotice> getInfo(@RequestParam(value = "id") Long noticeId) {
        return Resp.ok(noticeService.getById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @RamResource(action = "sys:notice:save", name = "保存通知公告", type = ActionType.WRITE)
    @PostMapping("/save")
    public Resp<?> save(@Validated @RequestBody SysNotice notice) {
        return noticeService.saveNotice(notice) ? Resp.ok() : Resp.fail("新增失败");
    }

    /**
     * 修改通知公告
     */
    @PostMapping("/update")
    @RamResource(action = "sys:notice:update", name = "更新通知公告", type = ActionType.WRITE)
    public Resp<?> update(@Validated @RequestBody SysNotice notice) {
        return noticeService.updateNotice(notice) ? Resp.ok() : Resp.fail("更新失败");
    }

    /**
     * 删除通知公告
     */
    @RamResource(action = "sys:notice:delete", name = "删除通知公告", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long noticeIds) {
        return noticeService.deleteNotice(noticeIds) ? Resp.ok() : Resp.fail("删除失败");
    }

    /**
     * 批量删除通知公告
     */
    @RamResource(action = "sys:notice:delete", name = "删除通知公告", type = ActionType.WRITE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] noticeIds) {
        return noticeService.deleteBatchNotice(noticeIds) ? Resp.ok() : Resp.fail("删除失败");
    }
}